define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'fangjian/index' + location.search,
                    add_url: 'fangjian/add',
                    edit_url: 'fangjian/edit',
                    del_url: 'fangjian/del',
                    multi_url: 'fangjian/multi',
                    import_url: 'fangjian/import',
                    table: 'fangjian',
                    closing: 'fangjian/closing',
                    opening: 'fangjian/opening',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'type', title: __('Type'), operate: 'LIKE'},
                        {field: 'price', title: __('Price'), operate: 'BETWEEN'},
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        // {field: 'category_id', title: __('Category_id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {
                            field: 'image',
                            title: __('Image'),
                            operate: false,
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.image
                        },
                        {field: 'biaoqian', title: __('Biaoqian'), operate: 'LIKE'},
                        // {field: 'xing', title: __('Xing'), operate: 'LIKE'},
                        {field: 'yuding', title: __('Yuding'), operate: 'LIKE'},
                        {
                            field: 'images',
                            title: __('Images'),
                            operate: false,
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.images
                        },
                        {field: 'category.name', title: __('门店名称'), operate: 'LIKE'},
                        {field: 'category.dizhi', title: __('Category.dizhi'), operate: 'LIKE'},
                        {field: 'category.phone', title: __('Category.phone'), operate: 'LIKE'},
                        {
                            field: 'is_dasao',
                            title: __('是否打扫'),
                            searchList: {0: __('否'), 1: __('是')},
                            formatter: Table.api.formatter.status
                        },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [{
                                name: 'upload',
                                title: __('Upload'),
                                text: '开电',
                                icon: 'fa fa-link',
                                classname: 'btn btn-xs btn-info btn-ajax',
                                url: 'fangjian/opening',
                            }, {
                                name: 'upload',
                                title: __('Upload'),
                                text: '关电',
                                icon: 'fa fa-chain-broken',
                                classname: 'btn btn-xs btn-info  btn-ajax',
                                url: 'fangjian/closing',
                            }, {
                                name: 'upload',
                                title: __('Upload'),
                                text: '测试喇叭',
                            
                                classname: 'btn btn-xs btn-info  btn-ajax',
                                url: 'fangjian/message',
                            }, {
                                name: 'upload',
                                title: __('Upload'),
                                text: '正在打扫',
                                classname: 'btn btn-xs btn-info  btn-ajax',
                                url: 'fangjian/dasaoing',
                                visible: function (row) {
                                    if (row.is_dasao == 0) {
                                        return true;
                                    } else {
                                        return false;
                                    }
                                }
                            }, {
                                name: 'upload',
                                title: __('Upload'),
                                text: '完成打扫',
                                classname: 'btn btn-xs btn-info  btn-ajax',
                                url: 'fangjian/dasaoer',
                                visible: function (row) {
                                    if (row.is_dasao == 1) {
                                        return true;
                                    } else {
                                        return false;
                                    }
                                }
                            }],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
